﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Configuration;
using System.Diagnostics;

namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowConnectionString.xaml
    /// </summary>
    public partial class WindowConnectionString : Window
    {
        public WindowConnectionString()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;

            try
            {

                string database = tbDatabase.Text.Trim();
                string uid = tbUid.Text.Trim();
                string password = tbPassword.Password.Trim();
                string server = tbServer.Text.Trim();

                string connectionString = "server=" + server + ";database=" + database + ";uid=" + uid + ";password=" + password + "";

                Configuration c = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
                ConnectionStringsSection section = (ConnectionStringsSection)c.GetSection("connectionStrings");
                section.ConnectionStrings["Recorders"].ConnectionString = connectionString;
                c.Save(ConfigurationSaveMode.Modified, true);
                ConfigurationManager.RefreshSection("connectionStrings");

                MakeConnectionStringDispay();

                //restart application
                Process.Start(Application.ResourceAssembly.Location);
                Application.Current.Shutdown();

                this.Close();
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }

        protected void InitializeTextBoxes()
        {
            try
            {
                Configuration c = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
                ConnectionStringsSection section = (ConnectionStringsSection)c.GetSection("connectionStrings");

                string connectionString = section.ConnectionStrings["Recorders"].ConnectionString;


                tbPassword.Password = connectionString.Substring(connectionString.LastIndexOf('=') + 1);
                connectionString = connectionString.Remove(connectionString.LastIndexOf(';'));

                tbUid.Text = connectionString.Substring(connectionString.LastIndexOf('=') + 1);
                connectionString = connectionString.Remove(connectionString.LastIndexOf(';'));

                tbDatabase.Text = connectionString.Substring(connectionString.LastIndexOf('=') + 1);
                connectionString = connectionString.Remove(connectionString.LastIndexOf(';'));

                tbServer.Text = connectionString.Substring(connectionString.LastIndexOf('=') + 1);

            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground= Brushes.Red;
            }

        }

        protected void MakeConnectionStringDispay()
        {
            try
            {
                Configuration c = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);
                ConnectionStringsSection section = (ConnectionStringsSection)c.GetSection("connectionStrings");
                string connectionString = section.ConnectionStrings["Recorders"].ConnectionString;

                int passLen = connectionString.Length - connectionString.LastIndexOf('=') - 1;

                string pass = "";

                for (int i = 0; i < passLen; i++)
                {
                    pass += "*";
                }


                lblConnectionString.Content = connectionString.Substring(0, connectionString.LastIndexOf('=')) + "=" + pass;
            }
            catch (Exception ex)
            {
                lblMessage.Content = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeTextBoxes();
            MakeConnectionStringDispay();
            lblMessage.Content = "";
        }

    }
}
